#include "form.h"
#include "ui_form.h"

trainer::trainer(QWidget *parent, int id) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    this->id = id;

    this->setWindowFlags(Qt::FramelessWindowHint);      // 隐藏边框
    this->setAttribute(Qt::WA_TranslucentBackground);     // 背景透明
    // 连接数据库
    dbase = new DbOperator();
    dbase->OpenDb();
    // 读取用户数据：
    msg = dbase->searchTrainerMsg(id);
    // 加载显示用户数据：
    showPersonMsg(msg);

}

trainer::~trainer()
{
    // 只有在最后父窗口关闭之后才会调用
    delete ui;
    if (dbase != 0) {
        delete dbase;
        dbase = 0;
    }
}

void trainer::closeEvent(QCloseEvent *) {
    emit ExitTrainer();   // 关闭时发射信号
}

void trainer::showPersonMsg(container msg)
{

    ui->username->setText(msg.username);
    ui->id->setText("id:"+QString::number(msg.id));
    ui->sex->setText("性别："+trans(msg.sex));
    ui->goldcoin->setText("金币数："+QString::number(msg.goldcoin));
    ui->knowledge->setText("知识点："+QString::number(msg.knowledge));
    ui->honor->setText("荣誉点："+QString::number(msg.honor));
}

QString trainer::trans(QString s)
{
    if (s == "men") {
        return QString("雄性");
    } else {
        return QString("雌性");
    }
}


void trainer::on_closebtn_clicked()
{
    this->close();
    delete dbase;     // 断开数据库连接
    dbase = 0;
}
